/* **********************************************************************
版本: 2022.3.62f1
作者: wzz
日期: 2025-8-1
描述: TouchSocketView
***********************************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;
using TMPro;
using System.Text;
using System;
using Framework;
using System.Threading.Tasks;

public class TouchSocketView : MonoBehaviour
{
    public static TouchSocketView Instance { get; private set; }

    public Button Btn_Connect;
    public Button Btn_Close;
    public Button Btn_SendMsg;
    public Button Btn_ClearMsg;
    public TMP_InputField Input_Ip;
    public TMP_InputField Input_Port;
    public TMP_InputField Input_Msg;
    public TextMeshProUGUI Text_Log;
    public TextMeshProUGUI Text_Time;
    public TextMeshProUGUI Text_HeadInfo;

    public RectTransform testRect;
    public Camera uiCamera;
    public Canvas canvas;

    private static StringBuilder stringBuilder = new StringBuilder();

    private void Start()
    {
        Application.runInBackground = true;

        Instance = this;
        InitEvent();
    }

    private void Update()
    {
        Text_Time.text = DateTime.Now.ToString("HH-mm-ss");

        if (NetMgr.Instance.FixDataAdpter != null)
        {
            var str = $"包头大小：{NetMgr.Instance.FixDataAdpter.HeadSize}";
            if (NetMgr.Instance.FixDataAdpter.ServerIsBigEndian)
                str = str + "  服务端为： 大端";
            else
                str = str + "  服务端为： 小端";
            Text_HeadInfo.text = str;
        }
    }

    private void InitEvent()
    {
        Btn_Connect.onClick.AddListener(OnBtnConnect);
        Btn_Close.onClick.AddListener(OnBtnClose);
        Btn_SendMsg.onClick.AddListener(OnBtnSendMsg);
        Btn_ClearMsg.onClick.AddListener(OnBtnClearMsg);
    }

    /// <summary>
    /// 连接
    /// </summary>
    private void OnBtnConnect()
    {
        int port;
        if (int.TryParse(Input_Port.text, out port))
        {
            NetMgr.Instance.ConnectAsync(Input_Ip.text, port);
        }
        else
        {
            AddLog("请输入正确的端口号");
        }
    }

    /// <summary>
    /// 关闭
    /// </summary>
    private void OnBtnClose()
    {
        //Close
        NetMgr.Instance.Destroy();
    }

    /// <summary>
    /// 发送消息
    /// </summary>
    private void OnBtnSendMsg()
    {
        var msg = Input_Msg.text;
        if (string.IsNullOrEmpty(msg))
            return;

        NetMgr.Instance.SendMsg(Encoding.UTF8.GetBytes(msg));
    }

    /// <summary>
    /// 清理信息
    /// </summary>
    private void OnBtnClearMsg()
    {
        stringBuilder.Clear();
        Text_Log.text = string.Empty;
    }

    public void AddLog(string msg)
    {
        msg = msg.Trim();
        if (string.IsNullOrEmpty(msg))
            return;

        string strTime = DateTime.Now.ToString("hh-mm--ss:ffff ->");
        stringBuilder.Append(strTime);
        stringBuilder.AppendLine(msg);

        Text_Log.text = stringBuilder.ToString();
        LogMoveToBottom();
    }

    private void LogMoveToBottom()
    {
        var log = Text_Log.transform as RectTransform;
        var content = Text_Log.transform.parent as RectTransform;
        var scrollView = content.parent.parent as RectTransform;
        var scrollViewH = scrollView.rect.height;

        LayoutRebuilder.ForceRebuildLayoutImmediate(content);

        if (log.rect.height < scrollViewH)
            content.localPosition = Vector3.zero;
        else
        {
            content.localPosition = new Vector3(0, log.rect.height - scrollViewH, 0);
        }
    }
}